<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/4/30
  Time: 10:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="../common.jsp"%>
<html>
<head>
    <title>报表测试</title>
</head>
<body>
<table id="reportGrid" class="easyui-datagrid" title="报表列表" style="width:100%;height:100%" toolbar="#tb">
    <thead>
    <tr>
        <th field="busId" width="10" hidden="true">项目ID</th>
        <th field="busName" width="150">项目名称</th>
        <th field="busPerson" width="150">项目负责人</th>
        <th field="busType" width="150" align="right">项目类型</th>
        <th field="busTime" width="150" align="right">申请时间</th>
    </tr>
    </thead>
</table>

<div id="tb">
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addReport()">新增报表</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editReport()">修改报表</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true" onclick="delReport()">删除报表</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="pauseReport()">打印报表</a>
</div>



<!--

<div id="report_edit" class="easyui-dialog" title="添加报表" data-options="iconCls:'icon-save'" style="border: 0; width: 500px;">
    <form id="mainForm" method="post">
        <input type="hidden" id="busId" name="busId"/>
        <table cellpadding="5">
            <tr>
                <td>项目名称:</td>
                <td><input class="easyui-textbox" type="text" name="busName" data-options="required:true"></input></td>
            </tr>
            <tr>
                <td>项目负责人:</td>
                <td><input class="easyui-textbox" type="text" name="busPerson" data-options="required:true"></input></td>
            </tr>
            <tr>
                <td>项目类型:</td>
                <td><input class="easyui-textbox" type="text" name="busType" data-options="required:true"></input></td>
            </tr>
            <tr>
                <td>项目内容:</td>
                <td style="width: 660px;height: auto">
                    <textarea style="width: 500px; height: 200px;overflow: scroll" name="busContent" data-options="required:true"></textarea>
                </td>
            </tr>
            <tr>
                <td>项目计划:</td>
                <td style="width: 660px;height: auto">
                    <textarea style="width: 500px; height: 200px;overflow: scroll" name="busPlan" data-options="required:true"></textarea>
                </td>
            </tr>
        </table>

    </form>
    <h4 style="margin-left: 350px; margin-top: 5px; margin-bottom: 2px;">
        项目经费情况（单位：元）
    </h4>
    <table id="tt" class="easyui-datagrid" title="预算列表" style="width:100%;height:100%">

    </table>
    <div style="text-align: center; padding: 5px">
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm('add')" id="addSub">添加</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm('update')" id="editSub">修改</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="closeForm()">取消</a>
    </div>

</div>

-->
<script type="application/javascript">

    function addReport(){
        $("#report_edit").dialog("open");
        $("#addSub").show();
        $("#editSub").hide();
        dataGrids();
    }

    function editReport(){
        var datagrid = $("#reportGrid").datagrid("getSelected");
        if(datagrid){
            $("#report_edit").dialog("open");
            $("#addSub").hide();
            $("#editSub").show();
            $.ajax({
                url:ctx+"/report/getReportById?busId="+datagrid.busId,
                type:'get',
                success:function (data) {
                    if(data.status){
                        $("#mainForm").form("load",data.respObj);
                        dataGrids();
                    }else{
                        $.messager.alert("操作提示","查询失败.","warning");
                    }
                },
                error:function (data) {
                    $.messager.alert("操作提示","响应失败.","warning");
                }

            });
        }else{
            $.messager.alert("警告","请选择操作的数据","error"); 
        }
    }

    function delReport(){
        var datagrid = $("#reportGrid").datagrid("getSelected");
        if(datagrid){
            $.messager.confirm("确认","您确认删除当前申请项目吗？",function (data) {
                if(data){
                    $.ajax({
                        url:ctx+"/report/deleteBusReport?busId="+datagrid.busId,
                        type:"get",
                        success:function(data){
                            if(data.status){
                                $("#reportGrid").datagrid('reload');
                                $.messager.confirm("操作提示",data.respMsg,"info");
                            }else{
                                $.messager.alert("警告",data.respMsg,"error");
                            }
                        },
                        error:function(data){
                            $.messager.alert("警告",data.respMsg,"error");
                        }
                    })
                }
            });
        }else{
            $.messager.alert("警告","请选择操作的数据","error");
        }

    }

    //打印功能
    function pauseReport(){
        var datagrid = $("#reportGrid").datagrid("getSelected");
        if(datagrid){
            window.location.href=ctx+"/report/prientBusReport?busId="+datagrid.busId;
        }else{
            $.messager.alert("警告","请选择操作的数据","error");
        }

    }
    //提交申请项目信息
    function submitForm(data){
        var url = "";
        var reqjson={};
        if(data=='add'){
            url=ctx+"/report/addBusReport";
        }else{
            url=ctx+"/report/updateBusReport";
        }
        var $dg = $('#tt');
        var inserted = $dg.datagrid('getChanges', "inserted");
        var deleted = $dg.datagrid('getChanges', "deleted");
        var updated = $dg.datagrid('getChanges', "updated");
        var effectRow = new Object();
        effectRow["inserted"] = JSON.stringify(inserted);
        effectRow["deleted"] = JSON.stringify(deleted);
        effectRow["updated"] = JSON.stringify(updated);
        reqjson={sysBusiness:$("#mainForm").serializeJSON(),
            sysBusinessExtend:JSON.parse(effectRow["inserted"]),
            sysBusinessExtendUpdate:JSON.parse(effectRow["updated"]),
            sysBusinessExtendDelete:JSON.parse(effectRow["deleted"])
        };
        $.messager.progress({
            title:'请稍后',
            msg:'执行中...'
        });
        $.ajax({
            url:url,
            type:"POST",   //post提交方式默认是get
            contentType : 'application/json;charset=utf-8',
            data:JSON.stringify(reqjson),   //序列化(传递对象)

            error:function(data) {
                // 设置表单提交出错
                $.messager.progress('close');
                $.messager.alert("操作提示",data.respMsg,"warning");
            },
            success:function(data) {
                if(data.status){
                    $.messager.progress('close');
                    $.messager.alert("操作提示",obj.respMsg,"info");
                    $("#report_edit").datagrid("close");
                    $("#reportGrid").datagrid("reload");
                    return;
                }
                $.messager.progress('close');
                $.messager.alert("操作提示",obj.respMsg,"warning");

            }
        })
    }

    function closeForm(){
        $("#report_edit").dialog("close");
    }
    var editcount = 0;

    $(function(){
        $("#report_edit").dialog("close");
        $("#reportGrid").datagrid({
            url:ctx+"/report/queryBusinessList",
            pagination:true,
            pageSize:20,
            pageList:[20,50,100],
            singleSelect:true,
            rownumbers:true,
            checkOnSelect:true,
            onBeforeLoad : function(param) {
                $.messager.progress({
                    text : '数据加载中....'
                });
            },
            onLoadSuccess : function(data) {
                $.messager.progress('close');
            },
            onLoadError : function() {
                $.messager.progress('close');
            }

        })
    });


    function saverow(index){
       $("#tt").datagrid("endEdit",index);
    }
    //取消保存
    function cancelrow(index){
       $("#tt").datagrid("cancelEdit",index);
    }
    //编辑费用款
    function editrow(index){
      $("#tt").datagrid("beginEdit",index);
    }

    function deleterow(){
       $.messager.confirm("确认","是否真的删除?",function(data){
           if(data){
               var selected = $("#tt").datagrid("getSelected");
               var index = $("#tt").datagrid("getRowIndex",selected);
               $("#tt").datagrid("deleteRow",index);
           }
           onLoadSuccessFooter();
       });
    }

    function dataGrids(){
        $("#tt").datagrid({
            url:ctx+"/report/queryBusExtendByBusId?busId="+$("#budId").val(),
            iconCls:'icon-edit',
            width:'100%',
            height:300,
            singleSelect:true,
            pageSize : 500,//默认选择的分页是每页5行数据
            pageList : [ 500, 1000, 1500, 2000, 2500, 3000 ],//可以选择的分页集合
            nowrap: true,
            striped: true,//设置为true将交替显示行背景。
            collapsible: false,//显示可折叠按钮
            loadMsg: '数据装载中......',
            singleSelect: true,//为true时只能选择单行
            fitColumns: true,//允许表格自动缩放，以适应父容器
            pagination: true,//分页
            collapsible: true,
            remoteSort:false,
            showFooter:true,
            columns:[[
                {field:'budgetId',title:'序号',width:140,editor:'numberbox',hidden:true,sortable:true},
                {field:'budgetName',title:'费用名称',width:140,editor:'textbox',sortable:true},
                {field:'budgetMoney',title:'费用金额',width:140,editor:{type:'numberbox',options:{precision:'2'}},sortable:true},
                {field:'budgetRemark',title:'费用描述',width:140,editor:'textbox',sortable:true},
                {field:'action',title:'操作',width:140,align:'center',
                    formatter:function(value,row,index){
                        if (row.editing){
                            var s = '<a href="javascript:void(0)" onclick="saverow('+index+')">保存</a> ';
                            var c = '<a href="javascript:void(0)" onclick="cancelrow('+index+')">取消</a>';
                            return s+c;
                        } else {
                            var e = '<a href="javascript:void(0)" onclick="editrow('+index+')">编辑</a> ';
                            var d = '<a href="javascript:void(0)" onclick="deleterow()">删除</a>';
                            return e+d;
                        }
                    }
                }
            ]],
            toolbar:[{text:"增加",iconCls:"icon-add", handler:addbudgetRow }],
            onLoadSuccess:function () {
                if(sta=='Y'){
                    $(".datagrid-toolbar").hide();
                    $("#tt").datagrid('hideCloumn','action');
                }
                onLoadSuccessFooter();
            },
            onBeforeEdit:function(index,row){
              row.editing = true;
              $("#tt").datagrid("refreshRow",index);
              editcount++;

            },
            onAfterEdit:function(index,row){
                row.editing=false;
                $("#tt").datagrid("refreshRow",index);
                editcount--;
            },
            onCacelEdit:function(index,row){
                row.editing=false;
                $("#tt").datagrid('refreshRow',index);
                editcount--;
            }
        })
    }
    function onLoadSuccessFooter(){
        var rows = $("#tt").datagrid("getFooterRows");
        rows[0]['budgetId']='合计';
        roew[0]['budgetMoney']=compute("budgetMoney");
        $("#tt").datagrid('reloadFooter');
    }

    /**计算预算费用的和*/
    function compute(column){
       var rows = $("#tt").datagrid("getRows");
       var total =0;
       for(var i=0;i<rows.length;i++){
           if(rows[i][column]==null||rows[i][column]==""){
               continue;
           }
           total+=parseFloat(rows[i][column]);
       }
       return total.toFixed(2);
    }

    function addbudgetRow(){
        if(editcount>0){
            //表示当前还要表格数据处在编辑状态下
            $.messager.alert("警告","当前还有"+editcount+"记录正在编辑,不能新增记录");
            return;
        }
        $("#tt").datagrid('appendRow',{
            budgetId:'',
            budgetName:'',
            budgetMoney:'',
            budgetRemark:''
        });

    }
</script>
</body>
</html>
